﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using WP7Client.Helpers;
using WP7Client.Controller;
using Microsoft.Phone.Controls.Maps;
using WP7Client.Resources;
using WP7Client.Classes;
using System.Windows.Data;
using Microsoft.Phone.Controls.Maps.Platform;
using System.Device.Location;

namespace WP7Client.UserInterface.Pages
{
    public partial class FriendNotificationPage : MSHPhoneApplicationPage
    {
        public static readonly DependencyProperty LongitudeProperty = DependencyProperty.Register("Longitude", typeof(Double), typeof(FriendNotificationPage), null);
        public static readonly DependencyProperty LatitudeProperty = DependencyProperty.Register("latitude", typeof(Double), typeof(FriendNotificationPage), null);

        private MapLayer pinLayer = new MapLayer();
        private Pushpin pin = new Pushpin();

        public FriendNotificationPage()
        {
            InitializeComponent();
            theMap.CredentialsProvider = new ApplicationIdCredentialsProvider(Configuration.MapCredentials);

            SetBinding(LongitudeProperty, new Binding("Notification.Longitude") { Mode = BindingMode.TwoWay });
            SetBinding(LatitudeProperty, new Binding("Notification.Latitude") { Mode = BindingMode.TwoWay });

            pinLayer = new MapLayer();
            pin = new Pushpin();

            pinLayer.Children.Add(pin);
            theMap.Children.Add(pinLayer);

            pin.Template = (ControlTemplate)theNotificationPage.Resources["pushPinTemplate"];
            pin.ApplyTemplate();
        }

        public void SetPosition()
        {
            pin.Location = new Location() { Longitude = this.Longitude, Latitude = this.Latitude };
            theMap.ZoomLevel = 5;
            theMap.Center = pin.Location;
        }

        public Double Longitude
        {
            get { return (Double)GetValue(LongitudeProperty); }
            set { SetValue(LongitudeProperty, value); }
        }

        public Double Latitude
        {
            get { return (Double)GetValue(LatitudeProperty); }
            set { SetValue(LatitudeProperty , value); }
        }

        private void MSHPhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            ControllerHelper.FriendNotificationController.FriendNotificationPage = this;
            DataContext = ControllerHelper.FriendNotificationController;
        }

        private void theMap_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point point = e.GetPosition(theMap);
            GeoCoordinate geoCoordinate = theMap.ViewportPointToLocation(point);
            if (geoCoordinate != null)
            {
                Latitude = pin.Location.Latitude = geoCoordinate.Latitude;
                Longitude = pin.Location.Longitude = geoCoordinate.Longitude;
                theMap.Center = geoCoordinate;
            }
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            RangTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
        }

    }
}
